package com.office.schedule.reminder.service.impl;

import java.util.List;

import com.office.common.utils.DateUtils;
import com.office.common.utils.SecurityUtils;
import com.office.schedule.manage.domain.Schedule;
import com.office.schedule.manage.mapper.ScheduleMapper;
import com.office.schedule.reminder.domain.ReminderEntity;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.office.schedule.reminder.mapper.ReminderMapper;
import com.office.schedule.reminder.service.IReminderService;

import javax.annotation.Resource;

/**
 * 日程提醒Service业务层处理
 *
 * @author 柒月
 * @date 2025-05-20
 */
@Service
public class ReminderServiceImpl implements IReminderService {

    @Resource
    private ReminderMapper reminderMapper;

    @Resource
    private ScheduleMapper scheduleMapper;

    /**
     * 查询日程提醒
     *
     * @param id 日程提醒主键
     * @return 日程提醒
     */
    @Override
    public ReminderEntity selectReminderById(Long id) {
        return reminderMapper.selectReminderById(id);
    }

    /**
     * 查询日程提醒列表
     *
     * @param reminder 日程提醒
     * @return 日程提醒
     */
    @Override
    public List<ReminderEntity> selectReminderList(ReminderEntity reminder) {
        // 获取当前登录用户
        Long loginUserId = SecurityUtils.getUserId();
        return reminderMapper.selectReminderList(reminder, loginUserId);
    }


    /**
     * 新增提醒日程
     *
     * @param reminder 提醒日程对象
     * @return 影响行数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertReminder(ReminderEntity reminder) {
        int i = reminderMapper.insertReminder(reminder);
        // 将日程表中的关联提醒表的id关联
        Schedule schedule = new Schedule();
        // 设置提醒表id
        schedule.setRemindId(reminder.getId());
        // 设置日程id
        schedule.setId(reminder.getScheduleId());
        scheduleMapper.updateSchedule(schedule);
        return i;
    }

    /**
     * @param reminder 修改提醒日程对象
     * @return 返回影响的行数
     */
    @Override
    public Integer updateReminder(ReminderEntity reminder) {
        // 更新时间
        reminder.setUpdateTime(DateUtils.getNowDate());
        return reminderMapper.updateReminder(reminder);
    }


    /**
     * @param ids 要删除提醒的id
     * @return 返回影响的行数
     */
    @Override
    public Integer deleteReminderByIds(Long[] ids) {
        return reminderMapper.deleteReminderByIds(ids);
    }
}
